#!/usr/bin/perl

use strict;
use warnings;

my $me = '"java -jar MyBot.jar"';
my @all_enemies = (
    '"java -jar versions/tokugawa-v14.jar"',
    #$me,
    #'"python tools/sample_bots/python/GreedyBot.py"',
    #'"python tools/sample_bots/python/HoldBot.py"',
    #'"python tools/sample_bots/python/HunterBot.py"',
    #'"python tools/sample_bots/python/HoldBot.py"',
    #'"python tools/sample_bots/python/LeftyBot.py"',
    #'"python tools/sample_bots/python/RandomBot.py"',
);

my $turns = 50;
$turns = $ARGV[0] if (@ARGV);
	
my $bot_count = 0;
my $map;
while ($bot_count < 2) {
    #$map = get_map();
    #$map = 'tools/maps/example/tutorial1.map'; 
	#$map = 'tools/maps/maze/maze_04p_01.map';
    #$map = 'tools/maps/random_walk/random_walk_10p_02.map';
    $map = 'tools/maps/cell_maze/cell_maze_p02_02.map';
    $bot_count = get_bot_count($map);
}
my $enemy_count = $bot_count - 1;
my @enemies = map { $all_enemies[int rand($#all_enemies)] } (1 .. $enemy_count);

print "map: $map\n";
print "bot count: $bot_count\n";
print "enemies: @enemies\n";
#print `head $map`;
#exit;

system('tools/playgame.py '
#    . '--capture_errors '
    . '--serial '
    . "--map_file $map "
    . '--log_dir game_logs '
    . "--turns $turns "
    . '--turntime=500 '
#    . '--scenario '
    . '--food random '
    . '--player_seed 42 '
    . '--end_wait=0.25 '
    . '--verbose -e '
    . $me . ' ' . join(' ', @enemies) . ' '
    );



sub get_map {
    #find tools/maps/ -iname *.map
    my @maps = `find tools/maps/ -iname *.map`;
    chomp(@maps);

    $maps[int rand($#maps)];
}

sub get_bot_count {
    my $map = shift;

    my $line = `grep players $map`;
    chomp $line;
    return 2 unless $line;

    (split ' ', $line)[1];
}
